﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

using Tomahawk.Runtime.Logic.UI;
using Tomahawk.Runtime.Rendering.Materials;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Components;
using Tomahawk.Runtime.Console;
using Microsoft.Xna.Framework.Graphics;

namespace Tomahawk.Runtime.Logic
{
    /// <summary>
    /// Default engine configuration class
    /// </summary>
    public class XEngineConfiguration : XObject
    {

        #region "Serializable Properties"

        /// <summary>
        /// Struct used to instance game manager automatically
        /// </summary>
        public class GameManagerReference
        {
            [XmlAttribute("managertype")]
            public string ManagerType = "";

            [XmlAttribute("managerarchetype")]
            public string ManagerArchetype = "";
        }

        [XmlElement("StyleManager")]
        public GameManagerReference _StyleManager = new GameManagerReference();
        
        public class OverlayConfiguration
        {
            [XmlElement("Framerate")]
            public FramerateParameters framerate = new FramerateParameters();

#if !XBOX
            [XmlElement("Profiler")]
            public ProfilerParameters profiler = new ProfilerParameters();
#endif

            [XmlElement("FreeCamera")]
            public FreeCamParameters freeCam = new FreeCamParameters();

            [XmlElement("Info")]
            public InfoParameters info = new InfoParameters();
        }

        [XmlElement("Overlays")]
        public OverlayConfiguration _Overlay = new OverlayConfiguration();

        [XmlElement("Console")]
        public ConsoleParameters _Console = new ConsoleParameters();

        public class VideoConfiguration
        {
            [XmlElement("VSync")]
            public bool vsync = false;

            [XmlElement("Multisampling")]
            public MultiSampleType multisampling = MultiSampleType.TwoSamples;

            [XmlElement("ScreenWidth")]
            public int screenWidth = 1280;

            [XmlElement("ScreenHeight")]
            public int screenHeight = 720;

            [XmlElement("VirtualCanvasWidth")]
            public int virtualCanvasWidth = 1280;

            [XmlElement("VirtualCanvasHeight")]
            public int virtualCanvasHeight = 720;

            [XmlElement("Fullscreen")]
            public bool fullscreen = false;

            [XmlElement("SceneShader")]
            public string sceneShaderName = @"Tomahawk.Content\Materials\Generics@scene_simple_Material";

            [XmlElement("ObjectShaderOverride")]
            public string objectShaderOverrideName = null;
        }

        [XmlElement("Video")]
        public VideoConfiguration _Video = new VideoConfiguration();
        
        public class AudioConfiguration
        {
            [XmlElement("Settings")]
            public string settings = "";
        }

        [XmlElement("Audio")]
        public AudioConfiguration _Audio = new AudioConfiguration();

        public class PhysicsConfiguration
        {
            [XmlElement("Enabled")]
            public bool enabled = false;
        }

        [XmlElement("Physics")]
        public PhysicsConfiguration _Physics = new PhysicsConfiguration();

        public class DebugConsoleConfiguration
        {
            [XmlAttribute("Enabled")]
            public bool enabled = false;
        }

        [XmlElement("DebugConsole")]
        public DebugConsoleConfiguration _DebugConsole = new DebugConsoleConfiguration();

        public class BSODConfiguration
        {
            [XmlAttribute("Enabled")]
            public bool Enabled = false;
        }

        [XmlElement("BSOD")]
        public BSODConfiguration _BSOD = new BSODConfiguration();

        public class GameLoopConfiguration
        {
            [XmlAttribute("FixedStep")]
            public bool FixedStep = false;

            [XmlAttribute("TargetElapsed")]
            public float TargetElapsed = 1.0f / 60.0f;
        }

        [XmlElement("GameLoop")]
        public GameLoopConfiguration _GameLoop = new GameLoopConfiguration();

        public class FallbackConfiguration
        {
            [XmlElement("Material")]
            public string MaterialName = @"Tomahawk.Content\Materials\Generics@fallback_Material";
        }

        [XmlElement("FallbackConfiguration")]
        public FallbackConfiguration _Fallback = new FallbackConfiguration();

        [XmlElement("SimulateTrialMode")]
        public bool SimulateTrialMode = false;

        [XmlElement("SerialBoostEnabled")]
        public bool SerialBoostEnabled = false;

        #endregion

        #region "Game Managers"

        private XStyleManager styleManager;
        
        public XStyleManager StyleManager
        {
            get { return this.styleManager; }
        }

        #endregion

        /// <summary>
        /// Utility method: Instance an object manager from a reference
        /// </summary>
        private XObject instanceManager(String name, GameManagerReference reference)
        {
            XObject returnObject = null;
            if ((reference.ManagerType != null) && (reference.ManagerType != ""))
            {
                Type managerType = Type.GetType(reference.ManagerType);
                returnObject = XWorld.Instance.CreateObject(managerType, name, reference.ManagerArchetype);
            }
            return returnObject;
        }

        /// <summary>
        /// Initialization
        /// </summary>
        protected override void OnInitialize()
        {
            base.OnInitialize();

            //Instance game managers
            this.styleManager = (XStyleManager)this.instanceManager("StyleManager", this._StyleManager);
        }

    }

}
